<template>
<el-dialog title="借阅" :close-on-click-modal="false" :visible.sync="visible" width="90%">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="读者编号" prop="readerId">
            <el-select v-model="dataForm.readerId" placeholder="请选择">
                <el-option v-for="item in readerCardList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="借阅时间" prop="borrowDate">
            <el-date-picker v-model="dataForm.borrowDate" type="date"></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="note">
            <el-input v-model="dataForm.note" placeholder="备注"></el-input>
        </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="width: 100%;">
        <el-table-column prop="img" header-align="center" align="center" label="封面" width="120">
            <template slot-scope="scope">
                <img v-if="scope.row.imgUrl" style="width: 100px; height: 100px" :src="scope.row.imgUrl" />
                <span v-else></span>
            </template>
        </el-table-column>
        <el-table-column prop="name" header-align="center" align="center" label="图书名称"></el-table-column>
        <el-table-column prop="type" header-align="center" align="center" label="分类" width="50"></el-table-column>
        <el-table-column prop="isbn" header-align="center" align="center" label="ISBN" width="130"></el-table-column>
        <el-table-column prop="author" header-align="center" align="center" label="作者"></el-table-column>
        <el-table-column prop="press" header-align="center" align="center" label="出版社"></el-table-column>
        <el-table-column prop="publishDate" header-align="center" align="center" label="出版时间" width="100">
            <template slot-scope="scope">{{scope.row.publishDate | formatYearMonth}}</template>
        </el-table-column>
        <el-table-column prop="language" header-align="center" align="center" label="语言" width="50"></el-table-column>
        <el-table-column prop="price" header-align="center" align="center" label="定价" width="80">
            <template slot-scope="scope">￥{{scope.row.price | formatPrice}}</template>
        </el-table-column>
        <el-table-column prop="upNum" header-align="center" align="center" label="在架数量" width="80"></el-table-column>
        <el-table-column header-align="center" align="center" label="备注" width="120">
            <template slot-scope="scope">
                <el-input v-model="scope.row.note"></el-input>
            </template>
        </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            readerCardList: [{
                id: '202010200002',
                desc: "202010200002"
            }],
            dataList: [],
            dataForm: {
                id: 0,
                readerId: "",
                borrowDate: new Date(),
                note: "",
                bookList: []
            },
            dataRule: {
                readerId: [{
                    required: true,
                    message: "申请人不能为空",
                    trigger: "blur"
                }],
                borrowDate: [{
                    required: true,
                    message: "申请时间不能为空",
                    trigger: "blur"
                }]
            }
        };
    },
    methods: {
        init(bookList) {
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["dataForm"].resetFields();
                this.dataList = bookList;
            });
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    let bookList = [];
                    this.dataList.forEach(function (item) {
                        let book = {
                            bookId: item.id,
                            note: item.note
                        }
                        bookList.push(book);
                    });
                    this.$http({
                        url: this.$http.adornUrl(`/generator/tborrow/save`),
                        method: "post",
                        data: this.$http.adornData({
                            readerId: this.dataForm.readerId,
                            note: this.dataForm.note,
                            bookList: bookList
                        })
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: "借阅成功",
                                type: "success",
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit("refreshDataList");
                                }
                            });
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                }
            });
        },
    }
};
</script>
